/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.ejweixin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import me.zhengjie.modules.ejweixin.domain.NgConstructionProgress;
import me.zhengjie.modules.ejweixin.domain.NgConstructionProgressComment;
import me.zhengjie.modules.ejweixin.domain.NgConstructionProgressPhtots;
import me.zhengjie.modules.ejweixin.mapper.NgConstructionProgressCommentMapper;
import me.zhengjie.modules.ejweixin.mapper.NgConstructionProgressPhtotsMapper;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.ejweixin.service.NgConstructionProgressService;
import me.zhengjie.modules.ejweixin.domain.vo.NgConstructionProgressQueryCriteria;
import me.zhengjie.modules.ejweixin.mapper.NgConstructionProgressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author cy
* @date 2025-04-23
**/
@Service
@RequiredArgsConstructor
public class NgConstructionProgressServiceImpl extends ServiceImpl<NgConstructionProgressMapper, NgConstructionProgress> implements NgConstructionProgressService {

    private final NgConstructionProgressMapper ngConstructionProgressMapper;

    @Autowired
    private NgConstructionProgressPhtotsMapper constructionProgressPhtotsMapper;

    @Autowired
    private NgConstructionProgressCommentMapper constructionProgressCommentMapper;

    @Override
    public PageResult<NgConstructionProgress> queryAll(NgConstructionProgressQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(ngConstructionProgressMapper.findAll(criteria, page));
    }

    @Override
    public PageResult<NgConstructionProgress> progressList(NgConstructionProgressQueryCriteria criteria, Page<Object> page) {
        IPage<NgConstructionProgress> pageList =  ngConstructionProgressMapper.findAll(criteria, page);
        List<NgConstructionProgress> list = pageList.getRecords();
        for(NgConstructionProgress data:list){
            List<NgConstructionProgressPhtots> ob = constructionProgressPhtotsMapper.findByProgressId(data.getId());
            data.setPhotoList(ob);
            List<NgConstructionProgressComment> comment = constructionProgressCommentMapper.selectByProgressId(data.getId());
            data.setCommentList(comment);
        }
        pageList.setRecords(list);
        return PageUtil.toPage(pageList);
    }

    @Override
    public List<NgConstructionProgress> queryAll(NgConstructionProgressQueryCriteria criteria){
        return ngConstructionProgressMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(NgConstructionProgress resources) {
        resources.setCreateTime(DateUtil.getTimeStamp());
        save(resources);
        List<NgConstructionProgressPhtots> photoList = resources.getPhotoList();
        try {
            ngConstructionProgressMapper.updateConstructionStatus(resources.getSiteId(), Integer.valueOf(resources.getProgress()));
        }catch (Exception e){
            e.printStackTrace();
        }
        for(NgConstructionProgressPhtots data:photoList){
            data.setProgressId(resources.getId());
            constructionProgressPhtotsMapper.insert(data);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NgConstructionProgress resources) {
        NgConstructionProgress ngConstructionProgress = getById(resources.getId());
        ngConstructionProgress.copy(resources);
        saveOrUpdate(ngConstructionProgress);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<NgConstructionProgress> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NgConstructionProgress ngConstructionProgress : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("人员ID", ngConstructionProgress.getStaffId());
            map.put("人员名称", ngConstructionProgress.getStaffName());
            map.put("人员职位", ngConstructionProgress.getStaffPosition());
            map.put("人员照片", ngConstructionProgress.getStaffPhotoPath());
            map.put("评论", ngConstructionProgress.getComment());
            map.put("点赞数", ngConstructionProgress.getLikes());
            map.put("回复数量", ngConstructionProgress.getReports());
            map.put("创建者", ngConstructionProgress.getCreateBy());
            map.put("更新者", ngConstructionProgress.getUpdateBy());
            map.put("创建日期", ngConstructionProgress.getCreateTime());
            map.put("更新时间", ngConstructionProgress.getUpdateTime());
            map.put("工地ID", ngConstructionProgress.getSiteId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}